package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyShiYanHuaRong;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * 安心分期-十堰华融API五选25
 *
 *  @author 李源聪
 */
@ApiPlan(productId = "54", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5ShiYanHuaRong implements ApiOrgPhoneMd5ConvertHandler {

    private static final String BRAND = "1265";
    private static final String SOURCE = "crxx-axfq";

    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("md5mobile", apiDataExchangeParameter.getPhoneMd5());
        newMap.put("brand", BRAND);
        return newMap;
    }

    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.str(jsonObject.getString("data"), "1")) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgApplyShiYanHuaRong apply = new OrgApplyShiYanHuaRong();
        apply.setName(apiApplyParameter.getUserName());
        apply.setMobile(apiApplyParameter.getPhone());
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            apply.setCity(city);
        }
        // 车
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        apply.setAge(apiFieldConverter.getAgeInt());
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无")
                    .when(apiApplyParameter.getCarInfo(), "2", "有")
                    .when(apiApplyParameter.getCarInfo(), "3", "有")
                    .when(apiApplyParameter.getCarInfo(), "4", "有")
                    .end("无");
            apply.setCar(car);
        }
        // 房
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "有")
                    .end("无");
            apply.setHouse(house);
        }
        // 保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String baodanIs = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                    .when(apiApplyParameter.getInsurancePolicy(), "2", "有")
                    .when(apiApplyParameter.getInsurancePolicy(), "3", "有")
                    .when(apiApplyParameter.getInsurancePolicy(), "4", "有")
                    .end("无");
            apply.setBaodan_is(baodanIs);
        }
        // 性别
        String sexChoose = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", "男")
                .when(apiApplyParameter.getSex(), "1", "女")
                .end();
        apply.setSex(sexChoose);
        // 贷款额度
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 3)
                .when(apiApplyParameter.getLoanAmount(), "2", 5)
                .when(apiApplyParameter.getLoanAmount(), "3", 10)
                .when(apiApplyParameter.getLoanAmount(), "4", 20)
                .end();
        apply.setMoney(money);
        // 来源
        apply.setSource(SOURCE);
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String shebao = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                    .when(apiApplyParameter.getSocialSecurity(), "2", "有")
                    .when(apiApplyParameter.getSocialSecurity(), "3", "有")
                    .when(apiApplyParameter.getSocialSecurity(), "4", "有")
                    .end("无");
            apply.setShebao(shebao);
        }
        // 公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无")
                    .when(apiApplyParameter.getProvidentFund(), "2", "有")
                    .when(apiApplyParameter.getProvidentFund(), "3", "有")
                    .when(apiApplyParameter.getProvidentFund(), "4", "有")
                    .end("无");
            apply.setGongjijin(gongjijin);
        }
        // 信用卡
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            String creditCard = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "1", "无")
                    .when(apiApplyParameter.getCreditLimit(), "2", "有")
                    .when(apiApplyParameter.getCreditLimit(), "3", "有")
                    .when(apiApplyParameter.getCreditLimit(), "4", "有")
                    .when(apiApplyParameter.getCreditLimit(), "5", "有")
                    .when(apiApplyParameter.getCreditLimit(), "6", "有")
                    .end("无");
            apply.setCredit_card(creditCard);
        }
        // 是否有企业
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            String bizLicenseYear = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", "无")
                    .when(apiApplyParameter.getBizLicenseYear(), "2", "有")
                    .when(apiApplyParameter.getBizLicenseYear(), "3", "有")
                    .when(apiApplyParameter.getBizLicenseYear(), "4", "有")
                    .end("无");
            apply.setCompany(bizLicenseYear);
        }
        // 芝麻分
        apply.setZhima(apiFieldConverter.getSesameCreditNumber());
        // 银行卡代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String isbankpay = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "是")
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", "否")
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", "否")
                    .end("否");
            apply.setIsbankpay(isbankpay);
        }

        apply.setTime((int)(System.currentTimeMillis() / 1000L));
        return apply;
    }

    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.str(jsonObject.getString("code"), "200")) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
